//
//  JYJRecommendTableViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/29.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJRecommendTableViewCell.h"
#import "BaseLabel.h"
#import "BaseButton.h"
#import "Masonry.h"
#import "JYJTAMacro.h"
#import "BaseCollectionView.h"
#import "JYJRecommendCollectionViewCell.h"
#import "JYJBestMuscicListModel.h"
#import "JYJNetWorkParser.h"
#import "JYJBestItemsModel.h"
#import "JYJVCOfDiscover.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
@interface JYJRecommendTableViewCell () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) BaseLabel *label;
@property (nonatomic, strong) BaseButton *button;
@property (nonatomic, strong) BaseCollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray <JYJBestItemsModel *> *marrOfModel;// 原model数组
@property (nonatomic, strong) NSMutableArray <JYJBestItemsModel *>*marr;// 接受4个随机model的数组
@property (nonatomic, strong) NSMutableArray <JYJBestItemsModel *>*modelOfMarr;// 接收原数组model 随机从这里取

@end
@implementation JYJRecommendTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createCollection];
        [self createSmallSubViews];
        [self handleData];
        
    }
    return self;
}
- (void)handleData {
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/discovery.jhtml" andBody:@"json=%7B%7D" andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfModel = [NSMutableArray array];
        self.marr = [NSMutableArray array];
        NSArray *listArray = result[@"data"][@"best_items"];
        for (NSDictionary *dic in listArray) {
            JYJBestItemsModel *model = [JYJBestItemsModel modelWithDic:dic];
            [self.marrOfModel addObject:model];
            self.modelOfMarr = self.marrOfModel;
        }
        // 随机取数据
        if (_modelOfMarr.count > 0) {
            for (int i = 0; i < 4; i++) {
                int num = arc4random() % (self.marrOfModel.count);
                [_marr addObject:self.modelOfMarr[num]];
                [self.marrOfModel removeObjectAtIndex:num];
            }
            
        }

        [self.collection reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

}

- (void)createCollection {
    _layout = [[UICollectionViewFlowLayout alloc]init];
    self.collection = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self.contentView addSubview:_collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[JYJRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"Recommend"];
    _collection.scrollEnabled = NO;
}
- (void)createSmallSubViews {
    self.image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"find_cat_header"]];
    [self.contentView addSubview:_image];
    self.label = [[BaseLabel alloc]init];
    [self.contentView addSubview:_label];
    _label.text = @"为您推荐";
    _label.textColor = [UIColor whiteColor];
    self.button = [BaseButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:_button];
    [_button setTitle:@"换一批" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(handelNextData:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 50) / 2,(SCREEN_WIDTH - 50) / 2 * 4 / 3);
    _layout.minimumLineSpacing = 20;
    _layout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    _collection.frame = CGRectMake(0, 45, SCREEN_WIDTH, self.contentView.frame.size.height - 45);
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
    }];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(13);
        make.left.equalTo(self.image.mas_right).with.offset(5);
        make.width.mas_equalTo (100);
        make.height.mas_equalTo(30);
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(18);
        make.right.equalTo(self.contentView.mas_right).with.offset(-10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(25);
    }];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJRecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Recommend" forIndexPath:indexPath];
    cell.backgroundColor =  [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
   
    JYJBestItemsModel *model = self.marr[indexPath.row];
    [cell setModel:model];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfDiscover *discover = (JYJVCOfDiscover *)  self.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder;
    discover.tabBarController.tabBar.hidden = YES;
    discover.hidesBottomBarWhenPushed = YES;
    JYJVCOfFeaturedTopThreeDetails *details = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
     JYJBestItemsModel *model = self.marr[indexPath.row];
    details.itemsModel = model;
    [discover.navigationController pushViewController:details animated:YES];
    discover.hidesBottomBarWhenPushed = NO;
    
}
- (void)handelNextData:(BaseButton *)button {
    [self.marr removeAllObjects];
    // 随机取数据
    NSInteger indexTwo = 0;
    NSInteger indexThree = 0;
    NSInteger indexFour = 0;
    NSInteger indexOne = arc4random() % _marrOfModel.count;
    [self.marr addObject:[_marrOfModel objectAtIndex:indexOne]];
    
    do {
        indexTwo = arc4random() %  _marrOfModel.count;
    } while (indexTwo == indexOne);
    [self.marr addObject:[ _marrOfModel objectAtIndex:indexTwo]];
    
    do {
        indexThree = arc4random() %  _marrOfModel.count;
    } while (indexThree == indexTwo || indexThree == indexOne);
    [self.marr addObject:[ _marrOfModel objectAtIndex:indexThree]];
    
    do {
        indexFour = arc4random() %  _marrOfModel.count;
    } while (indexFour == indexThree || indexFour == indexTwo || indexFour == indexOne);
    [self.marr addObject:[ _marrOfModel objectAtIndex:indexFour]];
    [self.collection reloadData];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
